package com.cyzy.chatai.config;

import com.cyzy.chatai.socket.ChatWebSocket;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final ChatWebSocket chatWebSocket;

    // 构造器注入
    public WebSocketConfig(ChatWebSocket chatWebSocket) {
        this.chatWebSocket = chatWebSocket;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatWebSocket, "/ws/chat")
                .setAllowedOrigins("*"); // 允许所有来源（开发环境）
    }
}
